package com.bell.atensettings.settingDetails;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.bell.atensettings.R;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class VersionInfoSettings extends Activity
{
    private String readLine(String file) throws IOException
    {
        BufferedReader reader = new BufferedReader(new FileReader(file), 256);
        try {
            return reader.readLine();
        } finally {
            reader.close();
        }
    }

    private String getKernelVersion()
    {
        try {
            String kernelinfo = readLine("/proc/version");
            Matcher matcher = Pattern
                    .compile(
                            "\\w+\\s+\\w+\\s+([^\\s]+)\\s+\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\s+([^\\s]+)\\s+(?:PREEMPT\\s+)?(.+)")
                    .matcher(kernelinfo);

            if (!matcher.matches()) {
                Log.e("DeviceInfoSettings", "Regex did not match on /proc/version: " + kernelinfo);
                return "Unavailable";
            } else if (matcher.groupCount() < 4) {
                Log.e("DeviceInfoSettings", "Regex match on /proc/version only returned " + matcher.groupCount()
                        + " groups");
                return "Unavailable";
            } else {
                return matcher.group(1);
            }
        } catch (IOException exception) {
            return "Unavailable";
        }

    }

    @Override
    protected void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.version_info);

        ((TextView) findViewById(R.id.tv_system_ver)).setText("android " + Build.VERSION.RELEASE);
        ((TextView) findViewById(R.id.tv_firmware_ver)).setText(getKernelVersion());
    }

    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }
}